From c4b5d6abed04b4c1248c9556c4358d75e0291af3 Mon Sep 17 00:00:00 2001 From: tsteven4 <13596209+tsteven4@users.noreply.github.com> Date: Fri, 18 Mar 2022 06:31:21 -0600 Subject: [PATCH] fix cross-origin resource sharing issues with map preview. (#871) * fix cross-origin resource sharing issues with map preview. This became critical in Qt 6.2.4 when using the file scheme with gmapbase.html. * Ensure QWebEngineSettings are set before page load. --- gui/map.cc | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/gui/map.cc b/gui/map.cc index c8ac0d76d..228959f21 100644 --- a/gui/map.cc +++ b/gui/map.cc @@ -34,6 +34,7 @@ #include // for QUrl #include // for QWebChannel #include // for QWebEnginePage +#include // for QWebEngineSettings, QWebEngineSettings::LocalContentCanAccessRemoteUrls #include // for QWebEngineView #include // for WaitCursor #include // for foreach @@ -102,6 +103,17 @@ Map::Map(QWidget* parent, baseUrl = QUrl("qrc:///gmapbase.html"); } + // If baseUrl is using the file scheme gmapbase.html will be local content, + // and it needs to be able to access https://maps.googleapis.com, which is + // remote. Before 6.2.4 qrc was also considered a local scheme. + // This changed with QTBUG-96849 in 6.2.4, + // https://github.com/qt/qtwebengine/commit/dc7c2962a83a5eeb3c08e1a7312458ea5a18f4a5. + // As of 6.2.4 if we don't set this flag we get net::ERR_NETWORK_ACCESS_DENIED with + // the file scheme, and the map preview doesn't work. + // In 6.2.3 we got a number of "from origin 'file://' has been blocked by CORS policy" + // messages with the file scheme, but the map preview seemed to work. + this->settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessRemoteUrls, true); + if (!fileName.isEmpty()) { QFile htmlFile(fileName); if (htmlFile.open(QIODevice::ReadOnly)) { -- 2.30.2